#include <QDebug>
#include <QtGui>
#include "customgroupbox.h"

CustomGroupBox::CustomGroupBox(QWidget *parent) :
    QGroupBox(parent),
    mGeo(NULL)
{
    setCheckable(true);
    setChecked(true);

    mAni = new QPropertyAnimation(this, "geometry", this);
    mAni->setDuration(600);

    mEffect = new QGraphicsColorizeEffect();
    mEffect->setColor(Qt::gray);

    connect(this, SIGNAL(clicked(bool)),
            this, SLOT(gotClick(bool)));
}

void CustomGroupBox::setup(QEasingCurve inCurve, QEasingCurve outCurve)
{
//    if (mAni!=NULL)
//    {
//        mAni->setEasingCurve();
    //    }

}

void CustomGroupBox::setChanged(const bool changed)
{
    LOG(objectName(), "setChanged:", changed?"true":"false");
    mHasChanges = changed;
    if (changed)
    {
        this->setGraphicsEffect(0);
    }
    else
    {
        this->setGraphicsEffect(mEffect);
    }
}

void CustomGroupBox::showAll()
{
    LOG(objectName(), "showAll", "");
    if (mGeo==NULL) return;

    mAni->setEasingCurve(QEasingCurve::InCubic);
    mAni->setStartValue(this->geometry());
    mAni->setEndValue(*mGeo);
    mAni->start();
}

void CustomGroupBox::hideAll()
{
    LOG(objectName(), "hideAll", "");
    // saving geometry
    if (mGeo==NULL)
    {
        mGeo = new QRect(this->geometry());
    }
    else
    {
        *mGeo = this->geometry();
    }
    QFont f(this->font());
    QFontMetrics fm(f);
    QRect r(this->geometry());
    r.setHeight(fm.height() + 2);

    mAni->setEasingCurve(QEasingCurve::OutCubic);
    mAni->setStartValue(this->geometry());
    mAni->setEndValue(r);
    mAni->start();
}

void CustomGroupBox::gotClick(bool checked)
{
    if(checked)
    {
        showAll();
    }
    else
    {
        hideAll();
    }
}


